package com.ysd.lis.controller.men;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysOrganization;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabDictitemVsChannelMapper;
import com.ysd.lis.mapper.lab.LabRptGroupMapper;
import com.ysd.lis.request.SysParamFieldValueDto;
import com.ysd.lis.request.SysParamParam;
import com.ysd.lis.request.men.*;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.SysOrganizationService;
import com.ysd.lis.service.SysParamValueService;
import com.ysd.lis.service.lab.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 检验仪器 前端控制器
 * </p>
 *
 * @author wanghao
 * @create 2024/4/24 0024
 */
@RestController
@RequestMapping("/men/Instr")
public class MenInstrController extends BaseController {

    @Autowired
    private LabRptresultImageService labRptresultImageServicel;

    @Autowired
    private LabRptresultService labRptresultService;

    @Autowired
    private LabReqMainService labReqMainService;
    @Autowired
    private LabInstrSetupService labInstrSetupService;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    LabReqdetailService labReqdetailService;
    @Autowired
    LabFeeitemVsItemDictService labFeeitemVsItemDictService;
    @Autowired
    LabChannelService labChannelService;

    @Autowired
    LabRptVsBarcodeService labRptVsBarcodeService;

    @Autowired
    private LabFeeItemService labFeeItemService;
    @Autowired
    private LabMaininfoService labMaininfoService;

    @Autowired
    private SysOrganizationService sysOrganizationService;
    @Autowired
    private LabRptGroupService labRptGroupService;
    @Autowired
    SysParamValueService sysParamValueService;

    @Autowired
    private LabDictitemVsChannelMapper dictitemVsChannelMapper;


    @ApiOperation("获取组织机构及基础信息")
    @PostMapping(value = "/getOrganization")
    public List<SysOrganizationVo> getOrganization() {
        List<SysOrganization> organizations = sysOrganizationService.list(new LambdaQueryWrapper<SysOrganization>().eq(SysOrganization::getDelFlag, 0));
        List<SysOrganizationVo> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(organizations)) {
            return new ArrayList<>();
        }
        for (SysOrganization organization : organizations) {
            SysOrganizationVo sysOrganizationVo = new SysOrganizationVo();
            sysOrganizationVo.setOrgId(organization.getId());
            sysOrganizationVo.setName(organization.getName());
            // 返回检验组信息
            List<Map<String, Object>> jygzzList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictListByOrgId("JYGZZ", organization.getId()).getData();
            if (CollectionUtils.isNotEmpty(jygzzList)) {
                sysOrganizationVo.setInsGroup(jygzzList);
            }
            // 返回报告单元
            List<LabRptGroup> rptGroups = labRptGroupService.list(new LambdaQueryWrapper<LabRptGroup>().eq(LabRptGroup::getDelFlag, 0).eq(LabRptGroup::getOrgId, organization.getId()));
            List<Map<String, String>> rptGrouList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(rptGroups)) {
                for (LabRptGroup rptGroup : rptGroups) {
                    Map<String, String> map = new HashMap<>();
                    map.put("name", rptGroup.getRptGroupName());
                    map.put("code", rptGroup.getRptGroup());
                    rptGrouList.add(map);
                }
            }
            sysOrganizationVo.setRptGroup(rptGrouList);
            // 添加授权模块
            List<Map<String, String>> moduleList = new ArrayList<>();
            Map<String, String> moduleMap = new HashMap<>();
            moduleMap.put("name", "检验系统");
            moduleMap.put("code", "insSysTem");
            moduleList.add(moduleMap);
            sysOrganizationVo.setModule(moduleList);
            list.add(sysOrganizationVo);
        }
        return list;
    }

    @ApiOperation("保存仪器信息")
    @PostMapping(value = "/saveInsInfo")
    public Result saveInsInfo(@RequestBody InstrSetupVo instrSetupVo) {
        List<LabInstrSetup> list = labInstrSetupService.list(new LambdaQueryWrapper<LabInstrSetup>()
                .eq(LabInstrSetup::getInstrCode, instrSetupVo.getInstrCode())
                .eq(LabInstrSetup::getOrgId, instrSetupVo.getOrgId())
                .eq(LabInstrSetup::getDelFlag, 0));
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.fail("该仪器已存在");
        }
        LabInstrSetup instrSetup = new LabInstrSetup();
        // 仪器大类是中文的,进行匹配,如果没有空着
        List<Map<String, Object>> yqdlList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictListByOrgId("YQDL", instrSetupVo.getOrgId()).getData();
        yqdlList.stream().filter(a -> (a.get("name").equals(instrSetupVo.getInstrClass()) || a.get("code").equals(instrSetupVo.getInstrClass()))).findFirst().ifPresent(stringObjectMap -> instrSetup.setInstrClass((String) stringObjectMap.get("code")));

        // 检验组传code,保持不变
        instrSetup.setWorkGroup(instrSetupVo.getInsGroup());
        // 报告单元传code,进行匹配,如果没有,维护报告单元,名称用仪器名称
        List<LabRptGroup> rptGroups = labRptGroupService.list(new LambdaQueryWrapper<LabRptGroup>().eq(LabRptGroup::getDelFlag, 0).eq(LabRptGroup::getRptGroup, instrSetupVo.getRptGroup()).eq(LabRptGroup::getOrgId, instrSetupVo.getOrgId()));
        instrSetup.setRptGroup(instrSetupVo.getRptGroup());
        if (CollectionUtils.isEmpty(rptGroups)) {
            LabRptGroup labRptGroup = new LabRptGroup();
            labRptGroup.setRptGroup(instrSetupVo.getRptGroup());
            labRptGroup.setRptGroupName(instrSetupVo.getInstrName());
            labRptGroup.setWorkGroup(instrSetupVo.getInsGroup());
            labRptGroup.setOrgId(instrSetupVo.getOrgId());
            labRptGroupService.save(labRptGroup);
        }
        instrSetup.setInstrCode(instrSetupVo.getInstrCode());
        instrSetup.setInstrName(instrSetupVo.getInstrName());
        instrSetup.setInstrCalled(instrSetupVo.getInstrName());
        instrSetup.setInstrMark(instrSetupVo.getInstrMark());
        instrSetup.setIsUse("1");
        instrSetup.setIsQc(true);
        instrSetup.setOrgId(instrSetupVo.getOrgId());

        labInstrSetupService.save(instrSetup);
        return Result.succ("维护成功");
    }

    @ApiOperation("仪器上传的图片结果")
    @PostMapping(value = "/uploadImageFiles")
    public Result uploadImageFiles(@RequestBody UploadImageDto uploadImageDto) {

        return labRptresultImageServicel.uploadImageFiles(uploadImageDto);

    }

    @ApiOperation("获取标本列表")
    @PostMapping(value = "/getSampleListAsync")
    public Result getSampleListAsync(@RequestBody TestInfoDto testInfoDto) {

        return labRptresultService.getSampleListAsync(testInfoDto);

    }

    @ApiOperation("签收获取双向通道号、标本号")
    @PostMapping(value = "/getChannelInfoBySign")
    public Map<String, Object> getChannelInfoBySign(@RequestBody MutualRecogDto mutualRecogDto) {

        String reqNo = mutualRecogDto.getReqNo();
        String instrCode = mutualRecogDto.getInstrCode();
        String orgId = mutualRecogDto.getOrgId();
        MutualRecogVo mutualRecogVo = new MutualRecogVo();

        if (StringUtils.isBlank(instrCode)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "仪器不能为空");
            map.put("data", new ArrayList<>());
            return map;
        }
        if (StringUtils.isBlank(reqNo)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "条码号不能为空");
            map.put("data", new ArrayList<>());
            return map;
        }
        List<Map<String, Object>> sexList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("Sex").getData();
        List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("PATTYPE").getData();
        List<MutualRecogVo> list = new ArrayList<>();


        LabInstrSetup instrSetup = labInstrSetupService.findRptGroupByInstrCode(instrCode, orgId);
        if (Objects.nonNull(instrSetup)) {
            String rptGroup = instrSetup.getRptGroup();
            LabReqMain reqMain = labReqMainService.list(new LambdaQueryWrapper<LabReqMain>().eq(LabReqMain::getReqNo, reqNo).eq(LabReqMain::getDelFlag, 0).eq(StringUtils.isNotBlank(orgId), LabReqMain::getOrgId, orgId)).get(0);
            mutualRecogVo.setReqNo(reqMain.getReqNo());
            mutualRecogVo.setPatNa(Objects.nonNull(reqMain.getPatNa()) ? reqMain.getPatNa() : "");
            mutualRecogVo.setPatNo(Objects.nonNull(reqMain.getPatNo()) ? reqMain.getPatNo() : "");
            sexList.stream().filter(a -> a.get("code").equals(reqMain.getSex())).findFirst().ifPresent(stringObjectMap -> mutualRecogVo.setSex((String) stringObjectMap.get("name")));
            mutualRecogVo.setSex(Objects.nonNull(mutualRecogVo.getSex()) ? mutualRecogVo.getSex() : "");
            patTypeList.stream().filter(a -> a.get("code").equals(reqMain.getPatType())).findFirst().ifPresent(stringObjectMap -> mutualRecogVo.setPatType((String) stringObjectMap.get("name")));
            mutualRecogVo.setPatType(Objects.nonNull(mutualRecogVo.getPatType()) ? mutualRecogVo.getPatType() : "");
            mutualRecogVo.setBirthDay(Objects.nonNull(reqMain.getBirthDay()) ? DateUtil.format(reqMain.getBirthDay(), "yyyy-MM-dd HH:mm:ss") : "");
            mutualRecogVo.setAge(Objects.nonNull(reqMain.getAge()) ? reqMain.getAge().toString() : "");
            mutualRecogVo.setAgeUnit(Objects.nonNull(reqMain.getAgeUnit()) ? reqMain.getAgeUnit() : "");
            mutualRecogVo.setSampleTypeNa(Objects.nonNull(reqMain.getSampleTypeNa()) ? reqMain.getSampleTypeNa() : "");
            mutualRecogVo.setReqTime(Objects.nonNull(reqMain.getReqTime()) ? DateUtil.format(reqMain.getReqTime(), "yyyy-MM-dd HH:mm:ss") : "");
            mutualRecogVo.setIsUrgent(Objects.nonNull(reqMain.getIsUrgent()) ? reqMain.getIsUrgent().toString() : "");
            mutualRecogVo.setReqDept(Objects.nonNull(reqMain.getReqDept()) ? reqMain.getReqDept() : "");
            mutualRecogVo.setReqDeptNa(Objects.nonNull(reqMain.getReqDeptNa()) ? reqMain.getReqDeptNa() : "");
            mutualRecogVo.setDiagnose(Objects.nonNull(reqMain.getDiagnose()) ? reqMain.getDiagnose() : "");
            List<LabReqdetail> reqdetails = labReqdetailService.list(new LambdaQueryWrapper<LabReqdetail>().eq(LabReqdetail::getReqNo, reqNo).eq(LabReqdetail::getDelFlag, 0).eq(StringUtils.isNotBlank(orgId), LabReqdetail::getOrgId, orgId));
            List<String> feeCodes = reqdetails.stream().filter(item -> StringUtils.isNotBlank(item.getFeeCode())).map(LabReqdetail::getFeeCode).collect(Collectors.toList());
            List<String> channeles = new ArrayList<>();
            if (CollectionUtils.isEmpty(feeCodes)) {
                mutualRecogVo.setHasData("0");
            } else {
                mutualRecogVo.setHasData("1");
                List<LabItemDict> itemDictListByFeeCoods = labFeeitemVsItemDictService.findItemDictListByFeeCoods(feeCodes, rptGroup, orgId);
                List<String> itemNoes = itemDictListByFeeCoods.stream().filter(item -> StringUtils.isNotBlank(item.getItemNo())).map(LabItemDict::getItemNo).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(itemNoes)) {
                    mutualRecogVo.setHasData("0");
                } else {
                    List<LabChannel> channels = labChannelService.findChannelListByItemNoes(itemNoes, instrCode, rptGroup, orgId);
                    if (CollectionUtils.isEmpty(channels)) {
                        mutualRecogVo.setHasData("0");
                    } else {
                        channeles = channels.stream().map(LabChannel::getChannelCode).distinct().collect(Collectors.toList());
                        mutualRecogVo.setChannel(String.join(",", channeles));
                    }
                }
            }
            list.add(mutualRecogVo);
            Map<String, Object> map = new HashMap<>();
            map.put("code", "1");
            map.put("msg", "请求成功，共{" + channeles.toArray().length + "}条通道号");
            map.put("data", list);
            return map;
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "未找到对应报告单元");
            map.put("data", list);
            return map;
        }
    }

    @ApiOperation("上机记录获取双向通道号、标本号")
    @PostMapping(value = "/getChannelInfoByMainInfo")
    public Map<String, Object> getChannelInfoByMainInfo(@RequestBody MutualRecogDto mutualRecogDto) throws JsonProcessingException {

        String reqNo = mutualRecogDto.getReqNo();
        String sampleNo = mutualRecogDto.getSampleNo();
        String instrCode = mutualRecogDto.getInstrCode();
        String orgId = mutualRecogDto.getOrgId();
        String sampleda = mutualRecogDto.getSampleda();

        MutualRecogVo mutualRecogVo = new MutualRecogVo();

        if (StringUtils.isBlank(instrCode)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "仪器不能为空");
            map.put("data", new ArrayList<>());
            return map;
        }
        if (StringUtils.isBlank(reqNo) && StringUtils.isBlank(sampleNo)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "条码号或标本号不能为空");
            map.put("data", new ArrayList<>());
            return map;
        }
        List<Map<String, Object>> sexList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("Sex").getData();
        List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("PATTYPE").getData();
        List<MutualRecogVo> list = new ArrayList<>();

        LabInstrSetup instrSetup = labInstrSetupService.findRptGroupByInstrCode(instrCode, orgId);
        if (Objects.nonNull(instrSetup)) {
            String rptGroup = instrSetup.getRptGroup();

            // 扫码上机时是否带出申请项目对照的报告项目
            SysParamParam sysp2 = new SysParamParam();
            sysp2.setParamCode("bgdyxtcs");
            sysp2.setField("tqhxmmrz");
            sysp2.setClassifyCode(rptGroup);
            List<SysParamFieldValueDto> sqxmdzbgxmLst = sysParamValueService.findSysParamFieldAndValueListByCode(sysp2);
            String tqhxmmrz = sqxmdzbgxmLst.size() <= 0 ? "" : sqxmdzbgxmLst.get(0).getValue();

            MPJLambdaWrapper<LabReqMain> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAll(LabReqMain.class);
            queryWrapper.selectAs(LabMaininfoQuery::getSampleNo, LabReqMain::getSampleNo);
            queryWrapper.leftJoin(LabMaininfoQuery.class, p -> p.eq(LabMaininfoQuery::getReqNo, LabReqMain::getReqNo).and(t -> t.ne(LabMaininfoQuery::getIsChk, 2).or().isNull(LabMaininfoQuery::getIsChk)).ge(LabMaininfoQuery::getSampleda, sampleda + " 00:00:00").le(LabMaininfoQuery::getSampleda, sampleda + " 23:59:59").eq(LabMaininfoQuery::getDelFlag, 0).eq(LabMaininfoQuery::getRptGroup, rptGroup));
            queryWrapper.eq(LabReqMain::getDelFlag, 0);
            queryWrapper.eq(StringUtils.isNotBlank(reqNo), LabReqMain::getReqNo, reqNo);
            queryWrapper.eq(StringUtils.isNotBlank(sampleNo), LabMaininfoQuery::getSampleNo, sampleNo);
            queryWrapper.eq(StringUtils.isNotBlank(orgId), LabReqMain::getOrgId, orgId);
            List<LabReqMain> reqMains = labReqMainService.list(queryWrapper);

            if (CollectionUtils.isEmpty(reqMains)) {
                Map<String, Object> map = new HashMap<>();
                map.put("code", "400");
                map.put("msg", "未查到条码号信息");
                map.put("data", list);
                return map;
            }
            LabReqMain reqMain = reqMains.get(0);
            List<String> sampleNoes = reqMains.stream().filter(item -> StringUtils.isNotBlank(item.getSampleNo())).map(LabReqMain::getSampleNo).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(sampleNoes)){
                Map<String, Object> map = new HashMap<>();
                map.put("code", "400");
                map.put("msg", "未找到标本号");
                map.put("data", list);
                return map;
            }

            mutualRecogVo.setReqNo(reqMain.getReqNo());
            mutualRecogVo.setPatNa(Objects.nonNull(reqMain.getPatNa()) ? reqMain.getPatNa() : "");
            mutualRecogVo.setPatNo(Objects.nonNull(reqMain.getPatNo()) ? reqMain.getPatNo() : "");
            sexList.stream().filter(a -> a.get("code").equals(reqMain.getSex())).findFirst().ifPresent(stringObjectMap -> mutualRecogVo.setSex((String) stringObjectMap.get("name")));
            mutualRecogVo.setSex(Objects.nonNull(mutualRecogVo.getSex()) ? mutualRecogVo.getSex() : "");
            patTypeList.stream().filter(a -> a.get("code").equals(reqMain.getPatType())).findFirst().ifPresent(stringObjectMap -> mutualRecogVo.setPatType((String) stringObjectMap.get("name")));
            mutualRecogVo.setPatType(Objects.nonNull(mutualRecogVo.getPatType()) ? mutualRecogVo.getPatType() : "");

            mutualRecogVo.setBirthDay(Objects.nonNull(reqMain.getBirthDay()) ? DateUtil.format(reqMain.getBirthDay(), "yyyy-MM-dd HH:mm:ss") : "");
            mutualRecogVo.setAge(Objects.nonNull(reqMain.getAge()) ? reqMain.getAge().toString() : "");
            mutualRecogVo.setAgeUnit(Objects.nonNull(reqMain.getAgeUnit()) ? reqMain.getAgeUnit() : "");
            mutualRecogVo.setSampleTypeNa(Objects.nonNull(reqMain.getSampleTypeNa()) ? reqMain.getSampleTypeNa() : "");
            mutualRecogVo.setReqTime(Objects.nonNull(reqMain.getReqTime()) ? DateUtil.format(reqMain.getReqTime(), "yyyy-MM-dd HH:mm:ss") : "");
            mutualRecogVo.setSampleNo(Objects.nonNull(reqMain.getSampleNo()) ? reqMain.getSampleNo() : "");
            mutualRecogVo.setIsUrgent(Objects.nonNull(reqMain.getIsUrgent()) ? reqMain.getIsUrgent().toString() : "");
            mutualRecogVo.setReqDept(Objects.nonNull(reqMain.getReqDept()) ? reqMain.getReqDept() : "");
            mutualRecogVo.setReqDeptNa(Objects.nonNull(reqMain.getReqDeptNa()) ? reqMain.getReqDeptNa() : "");
            mutualRecogVo.setDiagnose(Objects.nonNull(reqMain.getDiagnose()) ? reqMain.getDiagnose() : "");
            // List<LabReqdetail> reqdetails = labReqdetailService.list(new LambdaQueryWrapper<LabReqdetail>().eq(LabReqdetail::getReqNo, reqMain.getReqNo()).eq(LabReqdetail::getDelFlag, 0).eq(StringUtils.isNotBlank(orgId), LabReqdetail::getOrgId, orgId));
            // List<String> feeCodes = reqdetails.stream().filter(item -> StringUtils.isNotBlank(item.getFeeCode())).map(LabReqdetail::getFeeCode).collect(Collectors.toList());
            LambdaQueryWrapper<LabRptresult> rptresultLambdaQueryWrapper = new LambdaQueryWrapper<>();
            rptresultLambdaQueryWrapper.eq(LabRptresult::getDelFlag, 0);
            rptresultLambdaQueryWrapper.in(LabRptresult::getSampleNo, sampleNoes);
            rptresultLambdaQueryWrapper.eq(LabRptresult::getRptGroup, rptGroup);
            rptresultLambdaQueryWrapper.ge(LabRptresult::getSampleda, sampleda + " 00:00:00");
            rptresultLambdaQueryWrapper.le(LabRptresult::getSampleda, sampleda + " 23:59:59");
            rptresultLambdaQueryWrapper.and(item -> item.isNull(LabRptresult::getTestResult).or().eq(LabRptresult::getTestResult, tqhxmmrz).or().eq(LabRptresult::getTestResult, "").or().eq(LabRptresult::getTestResult, "复查"));
            List<LabRptresult> rptresultList = labRptresultService.list(rptresultLambdaQueryWrapper);
            List<String> channeles = new ArrayList<>();

            mutualRecogVo.setHasData("1");
            List<String> itemNoes = rptresultList.stream().filter(item -> StringUtils.isNotBlank(item.getItemNo())).map(LabRptresult::getItemNo).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(itemNoes)) {
                mutualRecogVo.setHasData("0");
            } else {
                List<LabChannel> channels = labChannelService.findChannelListByItemNoes(itemNoes, instrCode, rptGroup, orgId);
                if (CollectionUtils.isEmpty(channels)) {
                    mutualRecogVo.setHasData("0");
                } else {
                    channeles = channels.stream().map(LabChannel::getChannelCode).distinct().collect(Collectors.toList());
                    mutualRecogVo.setChannel(String.join(",", channeles));
                }
            }
            list.add(mutualRecogVo);
            Map<String, Object> map = new HashMap<>();
            map.put("code", "1");
            map.put("msg", "请求成功，共{" + channeles.toArray().length + "}条通道号");
            map.put("data", list);
            return map;
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "未找到对应报告单元");
            map.put("data", list);
            return map;
        }
    }

    @ApiOperation("上机记录获取收费项目及code")
    @PostMapping(value = "/getFeeInfoByMainInfo")
    public Map<String, Object> getFeeInfoByMainInfo(@RequestBody MutualRecogDto mutualRecogDto) {
        String sampleNo = mutualRecogDto.getSampleNo();
        String rptGroup = mutualRecogDto.getRptGroup();
        String orgId = mutualRecogDto.getOrgId();
        String sampleda = mutualRecogDto.getSampleda();

        if (StringUtils.isBlank(sampleNo)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "标本号或者条码号不能为空");
            map.put("data", new ArrayList<>());
            return map;
        }
        if (Objects.nonNull(rptGroup)) {
            LambdaQueryWrapper<LabMaininfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabMaininfo::getRptGroup, rptGroup);
            lambdaQueryWrapper.and(item->item.eq(LabMaininfo::getSampleNo, sampleNo).or().eq(LabMaininfo::getReqNo, sampleNo));
            lambdaQueryWrapper.ge(LabMaininfo::getSampleda, sampleda + " 00:00:00");
            lambdaQueryWrapper.le(LabMaininfo::getSampleda, sampleda + " 23:59:59");
            lambdaQueryWrapper.eq(LabMaininfo::getDelFlag, 0);
            List<LabMaininfo> list = labMaininfoService.list(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                List<LabReqdetail> reqdetails = labReqdetailService.list(new LambdaQueryWrapper<LabReqdetail>().eq(LabReqdetail::getReqNo, list.get(0).getReqNo()).eq(LabReqdetail::getDelFlag, 0).eq(StringUtils.isNotBlank(orgId), LabReqdetail::getOrgId, orgId));
                if (CollectionUtils.isEmpty(reqdetails)) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("code", "400");
                    map.put("msg", "未找到对应数据");
                    map.put("data", null);
                    return map;
                }
                List<String> feeCodes = reqdetails.stream().map(LabReqdetail::getFeeCode).filter(StringUtils::isNotBlank).collect(Collectors.toList());
                List<String> feeNames = reqdetails.stream().map(LabReqdetail::getFeeName).filter(StringUtils::isNotBlank).collect(Collectors.toList());
                FeeInfoVo feeInfoVo = new FeeInfoVo();
                feeInfoVo.setFeeCodes(String.join(",", feeCodes));
                feeInfoVo.setFeeNames(String.join(",", feeNames));
                List<FeeInfoVo> feeInfoVos= new ArrayList<>();
                feeInfoVos.add(feeInfoVo);
                Map<String, Object> map = new HashMap<>();
                map.put("code", "1");
                map.put("msg", "请求成功");
                map.put("data", feeInfoVos);
                return map;
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("code", "400");
                map.put("msg", "未找到对应上机记录");
                map.put("data", null);
                return map;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", "400");
        map.put("msg", "未找到对应报告单元");
        map.put("data", null);
        return map;
    }

    @ApiOperation("报告项目通道转译对照")
    @PostMapping(value = "/getDictitemVsChannelInfo")
    public Map<String, Object> getDictitemVsChannelInfo(@RequestBody MutualRecogDto mutualRecogDto) {
        String rptGroup = mutualRecogDto.getRptGroup();
        String instrCode = mutualRecogDto.getInstrCode();
        String orgId = mutualRecogDto.getOrgId();
        MPJLambdaWrapper<LabDictitemVsChannel> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabDictitemVsChannel.class);
        queryWrapper.leftJoin(LabItemDict.class, on -> on.eq(LabItemDict::getItemNo, LabDictitemVsChannel::getItemNo).eq(LabItemDict::getRptGroup, LabDictitemVsChannel::getRptGroup).eq(LabItemDict::getDelFlag, 0));
        queryWrapper.leftJoin(LabChannel.class, on -> on.eq(LabChannel::getChannelCode, LabDictitemVsChannel::getChannelCode).eq(LabChannel::getInstrCode, LabDictitemVsChannel::getInstrCode).eq(LabChannel::getRptGroup, LabDictitemVsChannel::getRptGroup).eq(LabChannel::getDelFlag, 0));
        queryWrapper.eq(LabDictitemVsChannel::getDelFlag, 0);
        queryWrapper.eq(LabItemDict::getIsTran, "1");
        queryWrapper.and(wrapper -> wrapper.eq(LabChannel::getIsStop, "0").or().isNull(LabChannel::getIsStop));
        queryWrapper.eq(LabDictitemVsChannel::getRptGroup, rptGroup);
        queryWrapper.eq(LabDictitemVsChannel::getInstrCode, instrCode);
        queryWrapper.eq(StringUtils.isNotBlank(orgId), LabDictitemVsChannel::getOrgId, orgId);
        List<LabDictitemVsChannel> labDictitemVsChannels = dictitemVsChannelMapper.selectJoinList(LabDictitemVsChannel.class, queryWrapper);
        // 根据报告项目进行分组
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(labDictitemVsChannels)) {
            map.put("code", "400");
            map.put("msg", "未找到对应数据");
            map.put("data", null);
            return map;
        }
        // 进行数据组装
        List<DictVsChannelVo> dictVsChannelVos = new ArrayList<>();
        for (LabDictitemVsChannel labDictitemVsChannel : labDictitemVsChannels) {
            DictVsChannelVo dictVsChannelVo = new DictVsChannelVo();
            dictVsChannelVo.setItemNo(labDictitemVsChannel.getItemNo());
            dictVsChannelVo.setChannelCode(labDictitemVsChannel.getChannelCode());
            dictVsChannelVo.setFeeCode(labDictitemVsChannel.getFeeCode());
            dictVsChannelVo.setFeeName(labDictitemVsChannel.getFeeName());
            dictVsChannelVo.setTranChannelCode(labDictitemVsChannel.getTranChannelCode());
            dictVsChannelVos.add(dictVsChannelVo);
        }
        map.put("code", "1");
        map.put("msg", "请求成功");
        map.put("data", dictVsChannelVos);
        return map;
    }

    @ApiOperation("签收获取标本列表")
    @PostMapping(value = "/getSampleListBySign")
    public Map<String, Object> getSampleListBySign(@RequestBody TestInfoDto testInfoDto) {

        Timestamp startTime = Timestamp.valueOf(testInfoDto.getReqTime() + " 00:00:00");
        Timestamp endTime = new Timestamp(System.currentTimeMillis());

        if (StringUtils.isBlank(testInfoDto.getOrgId())) {
            LambdaQueryWrapper<LabRptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabRptGroup::getRptGroup, testInfoDto.getRptGroup());
            lambdaQueryWrapper.eq(LabRptGroup::getDelFlag, 0);
            LabRptGroup labRptGroup = labRptGroupService.list(lambdaQueryWrapper).get(0);
            testInfoDto.setOrgId(StringUtils.isBlank(labRptGroup.getOrgId()) ? "" : labRptGroup.getOrgId());
        }

        // 根据报告单元获取申请项目
        MPJLambdaWrapper<LabFeeItem> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabFeeItem.class);
        lambdaQueryWrapper.leftJoin(LabFeeitemVsBarcode.class, p -> p.eq(LabFeeitemVsBarcode::getFeeCode, LabFeeItem::getFeeCode).eq(LabFeeitemVsBarcode::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(LabRptVsBarcode.class, p -> p.eq(LabRptVsBarcode::getBarcodeNo, LabFeeitemVsBarcode::getBarcodeNo).eq(LabRptVsBarcode::getDelFlag, 0));
        lambdaQueryWrapper.eq(LabRptVsBarcode::getRptGroup, testInfoDto.getRptGroup());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(testInfoDto.getOrgId()), LabFeeItem::getOrgId, testInfoDto.getOrgId());
        lambdaQueryWrapper.eq(LabFeeItem::getDelFlag, 0);
        List<LabFeeItem> labFeeItems = labFeeItemService.list(lambdaQueryWrapper);
        List<String> feeCodes = labFeeItems.stream().filter(item -> StringUtils.isNotBlank(item.getFeeCode())).map(LabFeeItem::getFeeCode).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(feeCodes)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "未找到对应项目编码");
            map.put("data", new ArrayList<>());
            return map;
        }

        MPJLambdaWrapper<LabReqMain> reqMainWrapper = new MPJLambdaWrapper<>();
        reqMainWrapper.selectAll(LabReqMain.class);
        reqMainWrapper.leftJoin(LabReqdetail.class, p -> p.eq(LabReqdetail::getReqNo, LabReqMain::getReqNo).eq(LabReqdetail::getDelFlag, 0).in(LabReqdetail::getFeeCode, feeCodes));
        reqMainWrapper.eq(LabReqMain::getDelFlag, 0);
        reqMainWrapper.eq(StringUtils.isNotBlank(testInfoDto.getOrgId()), LabReqMain::getOrgId, testInfoDto.getOrgId());
        reqMainWrapper.eq(LabReqMain::getIsSign, 1);
        reqMainWrapper.ge(LabReqMain::getReqTime, startTime);
        reqMainWrapper.le(LabReqMain::getReqTime, endTime);
        List<LabReqMain> reqMains = labReqMainService.list(reqMainWrapper);

        List<SampleVo> list = new ArrayList<>();
        List<LabReqMain> collectReqMain = reqMains.stream().filter(item -> StringUtils.isNotBlank(item.getReqNo())).collect(Collectors.toList());
        for (LabReqMain reqMain : collectReqMain) {
            SampleVo sampleVo = new SampleVo();
            sampleVo.setReqNo(reqMain.getReqNo());
            sampleVo.setPatNo(reqMain.getPatNo());
            sampleVo.setPatNa(reqMain.getPatNa());
            sampleVo.setReqTime(reqMain.getReqTime());
            list.add(sampleVo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", "1");
        map.put("msg", "请求成功");
        map.put("data", list);
        return map;
    }

    @ApiOperation("上机记录获取标本列表")
    @PostMapping(value = "/getSampleListByMainInfo")
    public Map<String, Object> getSampleListByMainInfo(@RequestBody TestInfoDto testInfoDto) {

        Timestamp startTime = Timestamp.valueOf(testInfoDto.getSampleda() + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(testInfoDto.getSampleda() + " 23:59:59");

        if (StringUtils.isBlank(testInfoDto.getOrgId())) {
            LambdaQueryWrapper<LabRptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabRptGroup::getRptGroup, testInfoDto.getRptGroup());
            lambdaQueryWrapper.eq(LabRptGroup::getDelFlag, 0);
            LabRptGroup labRptGroup = labRptGroupService.list(lambdaQueryWrapper).get(0);
            testInfoDto.setOrgId(StringUtils.isBlank(labRptGroup.getOrgId()) ? "" : labRptGroup.getOrgId());
        }


        MPJLambdaWrapper<LabMaininfo> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabMaininfo.class);
        queryWrapper.ge(LabMaininfo::getSampleda, startTime);
        queryWrapper.le(LabMaininfo::getSampleda, endTime);
        queryWrapper.eq(LabMaininfo::getDelFlag, 0);
        queryWrapper.eq(LabMaininfo::getRptGroup, testInfoDto.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(testInfoDto.getOrgId()), LabMaininfo::getOrgId, testInfoDto.getOrgId());
        queryWrapper.and(p -> p.eq(LabMaininfo::getIsDouble, 0).or().isNull(LabMaininfo::getIsDouble));
        queryWrapper.ne(LabMaininfo::getIsChk, 2);
        List<LabMaininfo> list = labMaininfoService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "400");
            map.put("msg", "无数据");
            map.put("data", new ArrayList<>());
            return map;
        } else {
            List<SampleVo> sampleVos = new ArrayList<>();
            List<LabMaininfo> collectMainInfo = list.stream().filter(item -> StringUtils.isNotBlank(item.getReqNo())).collect(Collectors.toList());

            for (LabMaininfo maininfo : collectMainInfo) {
                SampleVo sampleVo = new SampleVo();
                sampleVo.setReqNo(maininfo.getReqNo());
                sampleVo.setSampleNo(maininfo.getSampleNo());
                sampleVo.setPatNo(maininfo.getPatNo());
                sampleVo.setPatNa(maininfo.getPatNa());
                sampleVo.setReqTime(maininfo.getReqTime());
                sampleVo.setSampleda(maininfo.getSampleda());
                sampleVos.add(sampleVo);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("code", "1");
            map.put("msg", "请求成功");
            map.put("data", sampleVos);
            return map;
        }
    }

    @ApiOperation("确认标本执行")
    @PostMapping(value = "/completeSampleAsync")
    public Map<String, Object> completeSampleAsync(@RequestBody TestInfoDto testInfoDto) {

        return labRptresultService.completeSampleAsync(testInfoDto);

    }

}
